﻿var LCA = LCA || {};

LCA.Hotspot = function () {

    this.data = null;
    this.factorId = null;
    this.totalImpact = null;
    this.percent = null;
    this.defaultPercent = 0.3;

    this.init = function (data) {
        this.data = data;

    }

    this.initData = function (factorId, impact) {
        this.factorId = factorId;
        this.totalImpact = impact;

    }
    this.getHotspotDetails = function (percent) {

        this.setPercentage(percent);

        var result = [];
        // Rank, Item, Impact, Source
        for (var i = 0; i < this.data.length; i++) {
            var item = this.data[i];
            if (item.FactorId == this.factorId) {
                result = $.extend(true, [], item.Rows);

                break;
            }
        }

        // Sort first
        result = result.sort(function (a, b) {
            if (a == b)
                return 0;

            return a.Emission < b.Emission ? 1 : -1;

        });

        // Add Rank #
        for (var i = 0; i < result.length; i++) {
            var item = result[i];
            item.Rank = i + 1;
        }

        // Remove item if its emission doesn't exceed the limit
        var limit = this.percent * this.totalImpact;
        for (var i = result.length - 1; i >= 0; i--) {
            var item = result[i];
            if (item.Emission < limit) {
                result.splice(i, 1);
            }
        }


        // Make it only 3 items if it exceeds
        for (var i = result.length - 1; i >= 3; i--) {
            var item = result[i];
            result.splice(i, 1);
        }

        return result;
    }

    this.getFactorName = function () {

        for (var i = 0; i < this.data.length; i++) {
            var item = this.data[i];
            if (item.FactorId == this.factorId) {
                return item.FactorName;

            }
        }

        return "";
    }
    this.setPercentage = function (percent) {
        if (percent == null)
            this.percent = this.defaultPercent;
        else
            this.percent = percent;
    }
}